package com.linx.common.web.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import com.linx.common.core.utils.SpringUtils;
import com.linx.common.web.domain.bo.AddRequest;
import com.linx.common.web.domain.bo.DeleteRequest;
import com.linx.common.web.domain.bo.GetRequest;
import com.linx.common.web.domain.vo.AcsDeviceStatusVo;
import com.linx.common.web.domain.vo.AddAcpsAuthConfigVo;
import com.linx.common.web.domain.vo.DataVo;
import com.linx.common.web.domain.vo.ResponseVo;
import com.linx.common.web.properties.HkProperties;
import com.linx.common.web.properties.JcptGisProperties;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/***
 * @ClassName AcpsAuthConfigControlUtils
 * @Description 海康门禁权限操作工具类
 * @Author gushaoliang
 * @Date 2024/12/4 下午4:43
 * @Version 1.0
 */
@Component
@Slf4j
public class AcpsAuthConfigControlUtils {
    private String agreement;
    private String host;
    private String appKey;
    private String appSecret;
    private String artemisPath;


    public AcpsAuthConfigControlUtils() {
        HkProperties hkProperties = SpringUtils.getBean(HkProperties.class);
        agreement = hkProperties.getAgreement();
        host = hkProperties.getHost();
        appKey = hkProperties.getAppKey();
        appSecret = hkProperties.getAppSecret();
        artemisPath = hkProperties.getArtemisPath();
    }




    //添加权限配置
    public ResponseVo add(AddRequest addRequest) throws Exception {
        String apiPath = artemisPath + "/api/acps/v1/auth_config/add";
        Map<String, String> path = new HashMap<>(2);

        path.put(agreement + "://", apiPath);
        Map<String, String> headers = new HashMap<>();
        ArtemisConfig.host=host;
        ArtemisConfig.appKey=appKey;
        ArtemisConfig.appSecret=appSecret;
        //headers.put(tagId, "#tagId#");
        headers.put("tagId",addRequest.getTagId());
        String body = JSON.toJSONString(addRequest);
        String json = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, "application/json", headers);

        JSONObject jsonObject = JSONObject.parseObject(json);
        log.info("返回的json：" + jsonObject);
        ResponseVo<AddAcpsAuthConfigVo> responseVo = JSON.parseObject(json,
            new TypeReference<ResponseVo<AddAcpsAuthConfigVo>>() {
            });
        return responseVo;

    }
    //删除权限配置
    public  ResponseVo delete(DeleteRequest deleteRequest) throws Exception {
        String apiPath = artemisPath +"/api/acps/v1/auth_config/delete";
        Map<String, String> path = new HashMap<>(2);

        path.put(agreement + "://", apiPath);

        Map<String,String> headers = new HashMap<>();
        headers.put("tagId",deleteRequest.getTagId());
        ArtemisConfig.host=host;
        ArtemisConfig.appKey=appKey;
        ArtemisConfig.appSecret=appSecret;
        String body=JSON.toJSONString(deleteRequest);
        String json = ArtemisHttpUtil.doPostStringArtemis(path, body, null,  null,"application/json", headers);
        JSONObject jsonObject = JSONObject.parseObject(json);
        log.info("返回的json：" + jsonObject);
        ResponseVo<AddAcpsAuthConfigVo> responseVo = JSON.parseObject(json,
            new TypeReference<ResponseVo<AddAcpsAuthConfigVo>>() {
            });
        return responseVo;

    }

}
